cmake_minimum_required(VERSION 3.16)
project(InventorySystem VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 自动处理UI、MOC、RCC等
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

# 查找Qt6组件
find_package(Qt6 REQUIRED COMPONENTS Core Widgets Sql Charts)

# 设置Qt6模块
qt_standard_project_setup()

# 设置包含目录
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

# 源文件分组
set(HEADER_FILES
    include/login_dialog.h
    include/main_window.h
    include/product_model.h
    include/product_edit_dialog.h
    include/database_manager.h
    include/chart_manager.h
)

set(SOURCE_FILES
    src/login_dialog.cpp
    src/main_window.cpp
    src/product_model.cpp
    src/product_edit_dialog.cpp
    src/database_manager.cpp
    src/chart_manager.cpp
    main.cpp
)

set(UI_FILES
    ui/login_dialog.ui
    ui/main_window.ui
    ui/product_edit_dialog.ui
)

set(RESOURCE_FILES
    resources/icons.qrc
)

# 创建可执行文件
qt_add_executable(InventorySystem
    ${HEADER_FILES}
    ${SOURCE_FILES}
    ${UI_FILES}
    ${RESOURCE_FILES}
)

# 链接Qt6库
target_link_libraries(InventorySystem PRIVATE  Qt6::Core Qt6::Widgets Qt6::Sql Qt6::Charts)


# 设置包含目录
target_include_directories(InventorySystem PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# 设置UI文件目录，这样uic就知道在哪里查找.ui文件
set_property(SOURCE ${SOURCE_FILES} PROPERTY SKIP_AUTOUIC ON)

# 手动处理UI文件
qt6_wrap_ui(UI_HEADERS
    ui/login_dialog.ui
    ui/main_window.ui
    ui/product_edit_dialog.ui
)

# 将生成的UI头文件添加到目标
target_sources(InventorySystem PRIVATE ${UI_HEADERS})

# 设置包含生成的UI头文件的目录
target_include_directories(InventorySystem PRIVATE
    ${CMAKE_CURRENT_BINARY_DIR}
)
# 设置应用程序属性


# 在Windows上设置子系统
if(WIN32)
    set_target_properties(InventorySystem PROPERTIES
            WIN32_EXECUTABLE TRUE)

   #target_link_options(InventorySystem PRIVATE "LINKER:/SUBSYSTEM:WINDOWS")
endif()

# 安装目标
install(TARGETS InventorySystem
    RUNTIME DESTINATION bin
    BUNDLE DESTINATION .
)

# 安装资源文件
install(DIRECTORY resources/ DESTINATION resources)

# 在macOS上设置应用程序包属性
if(APPLE)
    set_target_properties(InventorySystem PROPERTIES
        MACOSX_BUNDLE_BUNDLE_NAME "进销存管理系统"
        MACOSX_BUNDLE_COPYRIGHT "Copyright © 2024 Company. All rights reserved."
        MACOSX_BUNDLE_GUI_IDENTIFIER "com.company.inventorysystem"
        MACOSX_BUNDLE_INFO_STRING "进销存管理系统 v1.0"
        MACOSX_BUNDLE_SHORT_VERSION_STRING "1.0.0"
        MACOSX_BUNDLE_LONG_VERSION_STRING "1.0.0"
    )
endif()

# 在Windows上设置图标
if(WIN32)
    set(APP_ICON "${CMAKE_CURRENT_SOURCE_DIR}/resources/icons/app_icon.ico")
    if(EXISTS ${APP_ICON})
        set_source_files_properties(${APP_ICON} PROPERTIES
            MACOSX_PACKAGE_LOCATION Resources
        )
        target_sources(InventorySystem PRIVATE ${APP_ICON})
    endif()
endif()

# 设置编译选项
if(MSVC)
    target_compile_options(InventorySystem PRIVATE /W4)
else()
    target_compile_options(InventorySystem PRIVATE -Wall -Wextra)
endif()

# 添加版本信息（Windows）
#if(WIN32)
#    set(VERSION_RESOURCE_FILE "${CMAKE_CURRENT_BINARY_DIR}/version.rc")
#    configure_file(
#        "${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.rc.in"
#        "${VERSION_RESOURCE_FILE}"
#        @ONLY
#    )
#    target_sources(InventorySystem PRIVATE "${VERSION_RESOURCE_FILE}")
#endif()

# 创建运行目标以方便测试
add_custom_target(run
    COMMAND InventorySystem
    DEPENDS InventorySystem
    COMMENT "运行进销存管理系统"
)
